package cn.zcx;

import java.util.concurrent.TimeUnit;

/**
 * description:死锁
 * author:zcx
 * lenovo
 * 时间：2022.02.08.08.46
 */
public class Ex5 {
    Object a=new Object();
    Object b=new Object();
    public static void main(String[] args) {
        var t=new Ex5();
        new Thread(t::m1,"T1").start();
        new Thread(t::m2,"T2").start();
    }
    void m1(){
        String tn=Thread.currentThread().getName();
        synchronized (a){
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (b){

                System.out.println(tn);
            }

        }

    }
    void m2(){
        String tn=Thread.currentThread().getName();
        synchronized (b){
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (a){
                System.out.println(tn);
            }
        }
    }
}
